/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CONTROLEUR;

import CONTROLEUR.exceptions.NonexistentEntityException;
import CONTROLEUR.exceptions.PreexistingEntityException;
import MODEL.Caracteriser;
import MODEL.CaracteriserPK;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import MODEL.Caracteristique;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author ahmed
 */
public class CaracteriserJpaController implements Serializable {

    public CaracteriserJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Caracteriser caracteriser) throws PreexistingEntityException, Exception {
        if (caracteriser.getCaracteriserPK() == null) {
            caracteriser.setCaracteriserPK(new CaracteriserPK());
        }
        caracteriser.getCaracteriserPK().setIdCar(caracteriser.getCaracteristique().getIdCar());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Caracteristique caracteristique = caracteriser.getCaracteristique();
            if (caracteristique != null) {
                caracteristique = em.getReference(caracteristique.getClass(), caracteristique.getIdCar());
                caracteriser.setCaracteristique(caracteristique);
            }
            em.persist(caracteriser);
            if (caracteristique != null) {
                caracteristique.getCaracteriserCollection().add(caracteriser);
                caracteristique = em.merge(caracteristique);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCaracteriser(caracteriser.getCaracteriserPK()) != null) {
                throw new PreexistingEntityException("Caracteriser " + caracteriser + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Caracteriser caracteriser) throws NonexistentEntityException, Exception {
        caracteriser.getCaracteriserPK().setIdCar(caracteriser.getCaracteristique().getIdCar());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Caracteriser persistentCaracteriser = em.find(Caracteriser.class, caracteriser.getCaracteriserPK());
            Caracteristique caracteristiqueOld = persistentCaracteriser.getCaracteristique();
            Caracteristique caracteristiqueNew = caracteriser.getCaracteristique();
            if (caracteristiqueNew != null) {
                caracteristiqueNew = em.getReference(caracteristiqueNew.getClass(), caracteristiqueNew.getIdCar());
                caracteriser.setCaracteristique(caracteristiqueNew);
            }
            caracteriser = em.merge(caracteriser);
            if (caracteristiqueOld != null && !caracteristiqueOld.equals(caracteristiqueNew)) {
                caracteristiqueOld.getCaracteriserCollection().remove(caracteriser);
                caracteristiqueOld = em.merge(caracteristiqueOld);
            }
            if (caracteristiqueNew != null && !caracteristiqueNew.equals(caracteristiqueOld)) {
                caracteristiqueNew.getCaracteriserCollection().add(caracteriser);
                caracteristiqueNew = em.merge(caracteristiqueNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                CaracteriserPK id = caracteriser.getCaracteriserPK();
                if (findCaracteriser(id) == null) {
                    throw new NonexistentEntityException("The caracteriser with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(CaracteriserPK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Caracteriser caracteriser;
            try {
                caracteriser = em.getReference(Caracteriser.class, id);
                caracteriser.getCaracteriserPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The caracteriser with id " + id + " no longer exists.", enfe);
            }
            Caracteristique caracteristique = caracteriser.getCaracteristique();
            if (caracteristique != null) {
                caracteristique.getCaracteriserCollection().remove(caracteriser);
                caracteristique = em.merge(caracteristique);
            }
            em.remove(caracteriser);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Caracteriser> findCaracteriserEntities() {
        return findCaracteriserEntities(true, -1, -1);
    }

    public List<Caracteriser> findCaracteriserEntities(int maxResults, int firstResult) {
        return findCaracteriserEntities(false, maxResults, firstResult);
    }

    private List<Caracteriser> findCaracteriserEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Caracteriser.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Caracteriser findCaracteriser(CaracteriserPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Caracteriser.class, id);
        } finally {
            em.close();
        }
    }

    public int getCaracteriserCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Caracteriser> rt = cq.from(Caracteriser.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
